<?php
namespace App\Libs\Captcha;

class SmsCaptcha extends \App\Libs\Base\Singleton
{
    public static $prefix = 'captcha:sms:';
    public function generate($mobile, $smsType = 'common') {
        $res = \App\Libs\Captcha\Algorithms\Numeric::instance()->generate();
        $engine = new \App\Libs\Views\Twig();
        $content = $engine->fetch(array('params' => array(
            'company' => COMPANY,
            'captcha' => $res['captcha'],
        ), 'tpl' => 'sms/' . $smsType . '.tpl.text'));

        // send captcha
        \App\Libs\Helper\SmsHelper::send($mobile, $content);
        
        // store check code
        return \App\Libs\Cache\Memcached::set(self::$prefix . $mobile, $res['check']);
    }
}
